import { _decorator, Component, Enum, Node, Sprite, SpriteFrame } from 'cc';
import { SHOPITEM } from '../AshFramework/Datas/Constant';
import { EventManager, MyEvent } from '../AshFramework/Managers/EventManager';
import { Shop } from './Shop';
import { AudioManager, Audios } from '../AshFramework/Managers/AudioManager';
const { ccclass, property } = _decorator;

@ccclass('ShopItem')
export class ShopItem extends Component {

    @property({ type: Enum(SHOPITEM) })
    Type: SHOPITEM = SHOPITEM.AMMO;

    @property(SpriteFrame)
    NormalSF: SpriteFrame = null;

    @property(SpriteFrame)
    CheckedSF: SpriteFrame = null;

    @property
    IsChecked: boolean = false;

    Sprite: Sprite = null;

    protected onLoad(): void {
        this.Sprite = this.getComponent(Sprite);
        this.node.on(Node.EventType.TOUCH_END, this.click, this);
        EventManager.on(MyEvent.SHOPITEM, this.unChecked, this);
    }

    protected start(): void {
        this.showSF();
    }

    showSF() {
        if (this.IsChecked) {
            this.Sprite.spriteFrame = this.CheckedSF;
        } else {
            this.Sprite.spriteFrame = this.NormalSF;
        }
    }

    click() {
        if (this.IsChecked) return;
        AudioManager.PlaySound(Audios.ButtonClick);
        EventManager.Scene.emit(MyEvent.SHOPITEM);
        this.IsChecked = true;
        this.showSF();
        Shop.Instance.switchPanel(this.Type);
    }

    unChecked() {
        this.IsChecked = false;
        this.showSF();
    }
}


